//
//  UIViewController+NavBar.m
//  青旅OA
//
//  Created by qlwl on 16/4/20.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "UIViewController+NavBar.h"
#import "UIButton+Action.h"
#import "UIButton+MyButton.h"
#import "UIImage+ColorImage.h"

@implementation UIViewController (NavBar)
- (void)getMyNavBarWithNavTitle:(NSString *) title andLeftBtnTitle:(NSString *)leftTitle andLeftIma:(NSString *)leftIma andRightTitle:(NSString *)rightTitle andRightIma:(NSString *)rightIma and:(navBtnClick)click;
{
    NSArray *leftImas=[leftIma componentsSeparatedByString:@";"];
    NSArray *rightImas=[rightIma componentsSeparatedByString:@";"];
    self.title=@"";
    if (title) {
//        UILabel * label=[[UILabel alloc] init];
//        label.text=title;
//        label.font=[UIFont systemFontOfSize:18];
//        label.textAlignment=NSTextAlignmentCenter;
//        label.textColor=[UIColor whiteColor];
//        self.navigationItem.titleView=label;
//        [label sizeToFit];
        self.title=title;
        NSDictionary* textAttributes = @{ NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]};
        self.navigationController.navigationBar.titleTextAttributes=textAttributes;
    }
    
    if ([UIImage imageNamed:[leftImas firstObject]]||leftTitle) {
        UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.frame=CGRectMake(0, 0, 40, 44);
        [leftBtn getMyBtnWithTitle:leftTitle andLeftIma:leftImas];
        leftBtn.tag=1;
        [leftBtn addTarget:^(UIButton *btn) {
            if (click) {
                click((int)btn.tag);
            }
        }];
        UIBarButtonItem * leftItem = [[UIBarButtonItem alloc]  initWithCustomView:leftBtn];
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSpacer.width = -10;
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftItem];
    }
    
    
    if ([UIImage imageNamed:[rightImas firstObject]]||rightTitle) {
        UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame=CGRectMake(0, 0, 40, 44);
        [rightBtn getMyBtnWithTitle:rightTitle andLeftIma:rightImas];
        rightBtn.tag=2;
        [rightBtn addTarget:^(UIButton *btn) {
            if (click) {
                click((int)btn.tag);
            }
        }];
        UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]  initWithCustomView:rightBtn];
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSpacer.width = -10;
        self.navigationItem.rightBarButtonItems = @[negativeSpacer,rightItem];
    }
    [self.navigationItem setHidesBackButton:YES];
    self.navigationItem.backBarButtonItem.title=@"";
    //设置返回按钮，如果左边有其他的按钮就不会显示返回按钮，如果没有就会显示返回按钮
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@""
                                   style:UIBarButtonItemStylePlain target:nil action:nil];
    backButton.tag=1;
    [self.navigationItem setBackBarButtonItem:backButton];
    self.navigationItem.backBarButtonItem.title=@"";
}
//只设置右边按钮，有返回按钮
- (void)getMyNavBarWithNavTitle:(NSString *) title andRightTitle:(NSString *)rightTitle andRightIma:(NSString *)rightIma and:(navBtnClick)click
{
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    [((UIButton *)(self.navigationItem.leftBarButtonItem.customView)) setTitle:nil forState:UIControlStateNormal];
    self.title=@"";
    if (title) {
//        UILabel * label=[[UILabel alloc] init];
//        label.text=title;
//        label.font=[UIFont systemFontOfSize:18];
//        label.textAlignment=NSTextAlignmentCenter;
//        label.textColor=[UIColor whiteColor];
//        self.navigationItem.titleView=label;
//        [label sizeToFit];
        
        self.title=title;
        NSDictionary* textAttributes = @{ NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
        self.navigationController.navigationBar.titleTextAttributes=textAttributes;
    }
    
    NSArray *rightImas=[rightIma componentsSeparatedByString:@";"];
    if ([UIImage imageNamed:[rightImas firstObject]]||rightTitle) {
        UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame=CGRectMake(0, 0, 40, 44);
        [rightBtn getMyBtnWithTitle:rightTitle andLeftIma:rightImas];
        rightBtn.tag=2;
        [rightBtn addTarget:^(UIButton *btn) {
            if (click) {
                click((int)btn.tag);
            }
        }];
        UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]  initWithCustomView:rightBtn];
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSpacer.width = -10;
        self.navigationItem.rightBarButtonItems = @[negativeSpacer,rightItem];
    }
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@""
                                   style:UIBarButtonItemStylePlain target:nil action:nil];
    backButton.tag=1;
    [self.navigationItem setBackBarButtonItem:backButton];
    self.navigationItem.backBarButtonItem.title=@"";
}
-(void)setNavBarImage:(UIImage *)image
{
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
//设置导航条颜色
- (void)setNavBarColor:(UIColor *)color
{
    if([[[UIDevice currentDevice]systemVersion] doubleValue]>=7.0){
        self.navigationController.navigationBar.barTintColor = color;
    }else{
        self.navigationController.navigationBar.tintColor = color;
    }
    NSDictionary* textAttributes = @{ NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]};
    self.navigationController.navigationBar.titleTextAttributes=textAttributes;
}
//设置导航条title
-(void)setNavTitle:(NSString *)title
{
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.title = title;
    NSDictionary* textAttributes = @{ NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]};
    self.navigationController.navigationBar.titleTextAttributes = textAttributes;
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    backButton.tag=1;
    [self.navigationItem setBackBarButtonItem:backButton];
    self.navigationItem.backBarButtonItem.title=@"";
}

- (void) setNavTitle:(NSString *)title andSEL:(SEL)sel;
{
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    self.title=title;
    NSDictionary* textAttributes = @{ NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]};
    self.navigationController.navigationBar.titleTextAttributes=textAttributes;
    if (self.navigationController.viewControllers.count>1) {
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
        backButton.tag=1;
        [self.navigationItem setBackBarButtonItem:backButton];
    }else{
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui左"] style:UIBarButtonItemStylePlain target:self action:sel];
        backButton.tag=1;
        self.navigationItem.leftBarButtonItem=backButton;
    }
    self.navigationItem.backBarButtonItem.title=@"";
}

+ (UIImageView *)findHairlineImageViewUnder:(UIView *)view  {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
- (UIImageView *) hiddenHairlineImageView
{
    UIImageView *imaView=[[self class] findHairlineImageViewUnder:self.navigationController.navigationBar];
    imaView.hidden=YES;
    return imaView;
}
- (UIImageView *) showHairlineImageView
{
    UIImageView *imaView=[[self class] findHairlineImageViewUnder:self.navigationController.navigationBar];
    imaView.hidden=NO;
    return imaView;
}
- (UIImageView *)setNavBarImaColor:(UIColor *)color;
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:color] forBarMetrics:UIBarMetricsDefault];
    return [self hiddenHairlineImageView];
}
@end
